'use strict';
/**
 * 合同性质为以下,增加"交付信息"菜单栏目
 * 云客签约-直属区域 : 7f83719a-fce8-11e4-bed8-00155d02c832
 * 明源签约-直属区域 : 54f8a2b7-fce7-11e4-bed8-00155d02c832
 */
define(function (require, exports, module) {
    var angular = require('angular');
    var util = require('@yunke/core/util').default;
    var NavManager = require('@yunke/core/public_menu/navManager');

    require('./contract-common');

    var ContractMenu = angular.module('ContractMenu', ['ContractCommonService']);

    ContractMenu.service('contractMenuService', ['$rootScope', 'contractCommonService', function ($rootScope, contractCommonService) {
        return {
            menuInit: function (contractNaturesId) {
                var pContractId = util.getUrlPara('pid', location.hash.substring(1));
                var contractId = util.getUrlPara('id', location.hash.substring(1));

                function addQuery(pathname, query) {
                    var strArr = [];
                    for (var key in query) {
                        var value = query[key];
                        if (value) {
                            strArr.push(key + '=' + value);
                        }
                    }
                    return pathname + (strArr.length > 0 ? ('?' + strArr.join('&')) : '');
                }

                /**
                 * 判断合同性质,改变菜单
                 */
                var menuData = [
                    {
                        "id": "edit_menu_base",
                        "name": "基本信息",
                        "level": 1,
                        "iconCls": "",
                        "url": [addQuery("#/base", {id: contractId, pid: pContractId})],
                        "children": null
                    },
                    {
                        "id": "edit_menu_receivable",
                        "name": "应收信息",
                        "level": 1,
                        "iconCls": "",
                        "url": [addQuery("#/receivable", {id: contractId, pid: pContractId})],
                        "children": null,
                        isVisible: function () {
                            return location.hash.match(/\W(?=id=)/ig) ? true : false;
                        }
                    }
                ];

                var defineNaturesArr = ['7f83719a-fce8-11e4-bed8-00155d02c832', '54f8a2b7-fce7-11e4-bed8-00155d02c832'];

                if (!contractId) {
                    menuData.splice(6, 1);
                }

                if (contractNaturesId) {
                    if (contractNaturesId != defineNaturesArr[0] && contractNaturesId != defineNaturesArr[1]) {
                        menuData.splice(6, 1);

                        new NavManager($('#nav'), menuData, true);
                    }
                } else {
                    if (contractId) {
                        contractCommonService.getContract(contractId, function (data) {
                            var curPropertyId = data.contract.contract_property_id;
                            if (curPropertyId !== defineNaturesArr[0] && curPropertyId !== defineNaturesArr[1]) {
                                menuData.splice(6, 1);
                                new NavManager($('#nav'), menuData, true);
                            } else {
                                new NavManager($('#nav'), menuData, true);
                            }
                        });
                    } else {
                        new NavManager($('#nav'), menuData, true);
                    }
                }
            }
        }
    }]);

    module.exports = ContractMenu;
});
